package com.example.sys.speciality.service;

import com.example.core.mapping.Mapping;
import com.example.core.service.BaseServiceImpl;
import com.example.core.utils.StringUtils;
import com.example.core.utils.UserUtils;
import com.example.sys.speciality.dao.SysSpecialityDao;
import com.example.sys.speciality.mapping.SysSpecialityMapping;
import com.example.sys.speciality.po.SysSpeciality;
import com.example.sys.speciality.po.SysSpecialityCategory;
import com.example.sys.speciality.vo.SysSpecialityVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SysSpecialityServiceImpl extends BaseServiceImpl<SysSpecialityDao, SysSpeciality, SysSpecialityVo> implements SysSpecialityService {

    @Resource
    private SysSpecialityCategoryService sysSpecialityCategoryService;

    @Override
    public Class<? extends Mapping> getMappingClass() {
        return SysSpecialityMapping.class;
    }

    @Override
    public List<SysSpeciality> getSpecialityByUserId(String userId) throws Exception {
        return getBaseMapper().getSpecialityByUserId(userId);
    }


    /****
     * 分配专家专业信息
     * @param vo
     * @throws Exception
     */
    @Override
    public void setSpeciality(SysSpecialityVo vo) throws Exception {
        String userId = UserUtils.getUserId();
        if (StringUtils.isNotEmpty(vo.getUserId())) {
            userId = vo.getUserId();
        }
        this.clearByUserid(userId);
        if (!vo.getCategoryIds().isEmpty()) {
            for (String categoryId : vo.getCategoryIds()) {
                SysSpecialityCategory specialityCategory = this.sysSpecialityCategoryService.getById(categoryId);
                SysSpeciality sysSpeciality = new SysSpeciality();
                sysSpeciality.setUserId(userId);
                sysSpeciality.setCategoryId(categoryId);
                sysSpeciality.setCategoryName(specialityCategory.getName());
                this.add(sysSpeciality);
            }
        }
    }

    @Override
    public void setSpecialityByMP(List<SysSpeciality> sysSpecialities) throws Exception {
        String userId = UserUtils.getUserId();
        if (!sysSpecialities.isEmpty()) {
            for (SysSpeciality sysSpeciality : sysSpecialities) {
                SysSpecialityCategory specialityCategory = this.sysSpecialityCategoryService.getById(sysSpeciality.getId());
                SysSpeciality speciality = new SysSpeciality();
                speciality.setUserId(userId);
                speciality.setCategoryId(sysSpeciality.getId());
                speciality.setCategoryName(specialityCategory.getName());
                this.add(speciality);
            }
        }
    }

    @Override
    public void clearByUserid(String userId) throws Exception {
        getBaseMapper().clearByUserId(userId);
    }
}
